package com.ruoyi.web.controller.control;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.control.domain.ElectricityHistory;
import com.ruoyi.control.service.IElectricityHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 电力历史Controller
 * 
 * @author ruoyi
 * @date 2024-11-09
 */
@RestController
@RequestMapping("/control/ElectricityHistory")
public class ElectricityHistoryController extends BaseController
{
    @Autowired
    private IElectricityHistoryService electricityHistoryService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询电力历史列表
     */
    @PreAuthorize("@ss.hasPermi('control:ElectricityHistory:list')")
    @GetMapping("/list")
    public TableDataInfo list(ElectricityHistory electricityHistory)
    {
        startPage();
        List<ElectricityHistory> list = electricityHistoryService.selectElectricityHistoryList(electricityHistory);
        return getDataTable(list);
    }

    /**
     * 导出电力历史列表
     */
    @PreAuthorize("@ss.hasPermi('control:ElectricityHistory:export')")
    @Log(title = "电力历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElectricityHistory electricityHistory)
    {
        List<ElectricityHistory> list = electricityHistoryService.selectElectricityHistoryList(electricityHistory);
        ExcelUtil<ElectricityHistory> util = new ExcelUtil<ElectricityHistory>(ElectricityHistory.class);
        util.exportExcel(response, list, "电力历史数据");
    }

    /**
     * 获取电力历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('control:ElectricityHistory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(electricityHistoryService.selectElectricityHistoryById(id));
    }

    /**
     * 新增电力历史
     */
    @PreAuthorize("@ss.hasPermi('control:ElectricityHistory:add')")
    @Log(title = "电力历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElectricityHistory electricityHistory)
    {
        return toAjax(electricityHistoryService.insertElectricityHistory(electricityHistory));
    }

    /**
     * 修改电力历史
     */
    @PreAuthorize("@ss.hasPermi('control:ElectricityHistory:edit')")
    @Log(title = "电力历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElectricityHistory electricityHistory)
    {
        return toAjax(electricityHistoryService.updateElectricityHistory(electricityHistory));
    }

    /**
     * 删除电力历史
     */
    @PreAuthorize("@ss.hasPermi('control:ElectricityHistory:remove')")
    @Log(title = "电力历史", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(electricityHistoryService.deleteElectricityHistoryByIds(ids));
    }


    /**
     * 电力故障次数  1天
     */
    @GetMapping("/getElectricTotal")
    public int getElectricTotal()
    {
//        int total = electricityHistoryService.getElectricTotal();
//        return total;
        String gdKey1 = redisCache.getCacheObject("gdKey");
        Integer cachedValue = Integer.valueOf((gdKey1 == null ? "0" : gdKey1));
        return cachedValue;
    }
}
